/*  header-comment
/*  file   : /Users/arida/react-hrm-h5/src/modules/finish/index
/*  author : arida
/*  date   : 2017-6-4 18:25:27
/*  last   : 2017-9-6 18:50:6
*/
import React, { Component } from "react";
import PropTypes from "prop-types";
import { bindActionCreators } from "redux";
import { connect } from "react-redux";
import { mid2Url } from "@ali/ding-mediaid";

// stateless components
import PreEntry from "./preEntry";
import Employee from "./employee";

// actions
import { fetchCompanyInfo, getUser } from "../../actions/finish";
import { startEntry } from "../../actions/welcome/";

import { setTitle, close, setRight } from "../../utils/dingtalk.js";
import { controlBack } from "../../utils/native.js";

import "./index.less";

const defaultLogo =
  "https://gw.alicdn.com/tfs/TB1DzppXBUSMeJjy1zkXXaWmpXa-160-160.png";

//将store注入组件的props
const mapStateToProps = state => ({
  finish: state.FINISH_COMPANY,
  welcome: state.WELCOME_USER
});
//将action与dispatch进行绑定并注入组件的props
const mapDispatchToProps = dispatch => ({
  //区分绑定到props的action与传递的props
  actions: bindActionCreators(
    {
      dispatch,
      fetchCompanyInfo,
      getUser,
      startEntry
    },
    dispatch
  )
});

@connect(mapStateToProps, mapDispatchToProps)
export default class Finish extends Component {
  static defaultProps = {
    finish: {},
    welcome: {}
  };
  //propTypes 验证
  static propTypes = {
    finish: PropTypes.object.isRequired,
    welcome: PropTypes.object.isRequired,
    actions: PropTypes.shape({
      dispatch: PropTypes.func.isRequired,
      fetchCompanyInfo: PropTypes.func.isRequired,
      getUser: PropTypes.func.isRequired
    }).isRequired
  };

  constructor(props) {
    super(props);
    setTitle({
      title: ""
    });
    setRight({ show: false });
  }

  componentWillMount() {
    this.props.actions.fetchCompanyInfo();
    this.props.actions.getUser();
    controlBack(() => close());
  }

  componentDidMount() {
    this.props.actions.startEntry().then(res => {
    });
  }

  render() {
    const company = this.props.finish.company;
    // 在职员工就是编辑状态。也就是在职和非在职看到结果不一样
    if (company) {
      // avoid block scope by using var
      var {
        companyName = "",
        companyLogo = defaultLogo
      } = this.props.finish.company;
    }
    return (
      <PreEntry
        companyName={companyName}
        hasLogo={companyLogo ? true : false}
        companyLogo={
          mid2Url(companyLogo || "", { imageSize: "thumb" }) || defaultLogo
        }
      />
    );
  }
}
